SHELL := /bin/zsh

# 自包含 Makefile - 便捷开发任务
# 用法示例：
#   make venv
#   make install                # 使用默认阿里云镜像安装 requirements.txt
#   make install PYPI_MIRROR=https://pypi.org/simple  # 覆盖镜像
#   make new OUT="scripts/analysis_sales.py" NAME="销售分析" FORCE=1
#   make smoke
#   make run SCRIPT=scripts/analysis_sales.py ARGS="--help"
#   make clean

PY := python3
VENV := .venv
PIP := $(VENV)/bin/pip
PYBIN := $(VENV)/bin/python

# default number of jobs for parallel examples
N_JOBS ?= 4

.PHONY: new smoke run clean help


# Help target: explain common make targets and generator strategy

help:
	@echo "用法：make <目标> [变量=值]"
	@echo "常用目标：new, smoke, clean, help"
	@echo ""
	@echo "make new OUT=路径/脚本.py NAME=\"任务名\" [FORCE=1] [STANDALONE=1]"
	@echo "  默认行为：生成一个最小化的脚手架（不包含示例绘图或表格），便于在项目中按需实现主要逻辑。"
	@echo "    - 生成器会把一个小的 import header 写入文件头（若不使用 --standalone），然后追加脚手架主体。"
	@echo "    - 使用 STANDALONE=1 会在文件顶部内联常用的 bootstrap helpers（start/end/logging 等）。"
	@echo "  示例："
	@echo "    make new OUT=scripts/analysis_sales.py NAME=\"销售分析\" FORCE=1"
	@echo "    make new OUT=scripts/analysis_sales_standalone.py NAME=\"销售分析\" STANDALONE=1"
	@echo ""
	@echo "注意："
	@echo "  - 使用共享 helper 的生成脚本需要在运行时能访问到项目路径（生成的 import 头会将项目根加入 sys.path）。请确保在运行前已激活或准备好你的 Python 环境（例如 .venv）。"
	@echo "  - smoke 目标会优先使用项目虚拟环境下的 Python（.venv/bin/python），若不可用则退回到系统 Python。"
	@echo "  - Standalone 模式会将引导代码内联，生成的脚本更大，但无需依赖仓库环境即可运行。"


# Note: venv and install targets have been removed. Use your preferred
# environment management workflow (venv/conda/poetry) and run
# `./scripts/bootstrap_dev.sh` manually if needed.

# 生成 standalone 脚本：直接拼接 templates/bootstrap_snippet.py + templates/standalone_template.py
# 使用： make new OUT="scripts/analysis_sales.py" NAME="销售分析" [FORCE=1]


new:
	@if [ -z "$(NAME)" ]; then \
		echo "Usage: make new OUT=path/to/script.py NAME='任务名' [FORCE=1] [STANDALONE=1] (or PATH=... for backward compatibility)"; exit 1; \
	fi
	@if [ -z "$(OUT)" ] && [ -z "$(PATH)" ]; then \
		echo "Usage: make new OUT=path/to/script.py NAME='任务名' [FORCE=1] [STANDALONE=1] (or PATH=... for backward compatibility)"; exit 1; \
	fi
	@if [ -e "$(if $(OUT),$(OUT),$(PATH))" ] && [ "$(FORCE)" != "1" ]; then \
		echo "Destination $(if $(OUT),$(OUT),$(PATH)) exists; use FORCE=1 to overwrite"; exit 1; \
	fi
	@mkdir -p "$(dir $(if $(OUT),$(OUT),$(PATH)))"
	@chmod +x "$(CURDIR)/templates/new_script.sh" || true
	@chmod +x "$(CURDIR)/scripts/generate.sh" || true
	@bash "$(CURDIR)/templates/new_script.sh" "$(if $(OUT),$(OUT),$(PATH))" "$(NAME)" $(if $(FORCE),--force,) $(if $(STANDALONE),--standalone,) || true
	@chmod +x "$(if $(OUT),$(OUT),$(PATH))"
	@echo "Created $(if $(OUT),$(OUT),$(PATH)) (via generator)"


# Smoke 测试（生成并运行示例脚本）
smoke:
	@echo "[make] generating smoke script"
	@$(MAKE) new OUT="scripts/analysis_smoke.py" NAME="烟雾测试-图表" FORCE=1
	@echo "[make] running smoke script"
	@# Prefer project virtualenv python if available, otherwise fall back to system python
	@if [ -x "$(PYBIN)" ]; then \
		USE_PARALLEL="$(USE_PARALLEL)" N_JOBS="$(N_JOBS)" "$(PYBIN)" scripts/analysis_smoke.py || true; \
	else \
		USE_PARALLEL="$(USE_PARALLEL)" N_JOBS="$(N_JOBS)" $(PY) scripts/analysis_smoke.py || true; \
	fi


clean:
	@echo "[make] cleaning generated files"
	@rm -f scripts/analysis_smoke*.py scripts/*_test.py || true
	@rm -rf figures || true
	@find . -name "__pycache__" -type d -prune -exec rm -rf {} + || true
	@find . -name "*.pyc" -type f -delete || true
	@if [ "$(CLEAN_VENV)" = "1" ]; then rm -rf "$(VENV)" && echo "[make] removed virtualenv $(VENV)"; fi
	@echo "[make] clean complete"
